﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json.Linq;
using foursquare.Foursquare;
using WmAutoUpdate;
using foursquare.Forms;
using System.Threading;
using foursquare.Services;
using foursquare.Location;

namespace foursquare
{
    public partial class HomeView : Form
    {
        private static List<Checkin> checkins;

        private LocationManager _locationManager;
        private Thread _locationThread;
        private List<GeoService> _geoServices;
        private LocationTextService _formsService = new LocationTextService();
        private GeoLocation _previousLocation;

        const int kSplashUpdateInterval_ms = 200;
        const int kMinAmountOfSplashTime_ms = 8000;

        /// <summary>
        /// Instance of the splash form.
        /// </summary>
        static SplashView splash = null;

        /// <summary>
        /// Starts the splash screen on a separate thread
        /// </summary>
        static public void StartSplash()
        {
            // Instance a splash form given the image names
            splash = new SplashView(kSplashUpdateInterval_ms);

            // Run the form
            Application.Run(splash);
        }

        public HomeView()
        {
            InitializeComponent();

            // Create a new thread from which to start the splash screen form
            //Thread splashThread = new Thread(new ThreadStart(StartSplash));
            //splashThread.Start();

            BindData();

            _formsService.LocationChanged += new LocationChangedDelegate(_formsService_LocationChanged);

            _locationManager = new LocationManager();

            _locationThread = new Thread(new ThreadStart(LoadServices));
            _locationThread.Start();

            // Close the splash screen
            //CloseSplash();
        }

        /// <summary>
        /// Shuts down and cleans up the splash screen
        /// </summary>
        private void CloseSplash()
        {
            if (splash == null)
                return;

            // Shut down the splash screen
            splash.Invoke(new EventHandler(splash.KillMe));
            splash.Dispose();
            splash = null;
        }

        private void _formsService_LocationChanged(GeoLocation location)
        {
            if (this.InvokeRequired)
            {
                LocationChangedDelegate method = new LocationChangedDelegate(_formsService_LocationChanged);
                this.Invoke(method, new object[] { location });
            }
            else
            {
                lblLatitude.Text = "GPS on";
                //lblLatitude.Text = location.Latitude.ToString();
                _previousLocation = location;
            }
        }

        private void LoadServices()
        {
            _geoServices = new List<GeoService>()
            {
                _formsService
            };

            _locationManager.Attach(_formsService);
        }

        public void BindData()
        {
            try
            {
                if (checkins == null)
                    checkins = FS.GetCheckins();

                foreach (Checkin c in checkins)
                {
                    CheckinPanel cp = new CheckinPanel(c);
                    cp.Dock = DockStyle.Bottom;
                    this.Controls.Add(cp);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to connect to the network.  Try again later.");
            }
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            UserView view = new UserView(String.Empty);
            view.Show();
        }

        private void mnuCheckIn_Click(object sender, EventArgs e)
        {
            //Go to checkin page
            if (_previousLocation != null)
            {
                CheckinView view = new CheckinView(_previousLocation.Latitude, _previousLocation.Longitude);
                view.Show();
            }
            else
            {
                MessageBox.Show("Try again after position has been found.", this.Text);
            }
        }

        private void mnuRefresh_Click(object sender, EventArgs e)
        {
            checkins = null;
            BindData();
        }

        private void mnuExit_Click(object sender, EventArgs e)
        {
            _locationManager.Stop();
            Application.Exit();
        }
    }
}